/**
 * 
 */
package sudoku.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.Timer;

import sudoku.model.SudokuModel;
import sudoku.util.Language;

/**
 * @author Romet Piho
 *
 */
public class GenerationBarUI implements ActionListener {

	private volatile Language language;
	private volatile SudokuUI m2ng;
	private volatile SudokuModel model;
	
	private JButton cancel;
	private JProgressBar progressBar;
	private volatile Timer timer;
	
	public GenerationBarUI(Language language, SudokuUI m2ng, SudokuModel model) {
		this.language = language;
		this.m2ng = m2ng;
		this.model = model;
	}
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(timer)) {
			progressBar.setValue(progressBar.getValue()+5);
		}
		else if (e.getSource().equals(cancel)) {
			model.generate.done = true;
			model.setLevel(model.getLastLevel());
		}
	}
	
	public void drawWindow() {
		JDialog d = new JDialog(m2ng);
		d.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		c.gridx = 0;
		c.gridy = 0;		
		d.add(new JLabel(language.getController_genereeri()), c);
		
		c.gridx = 0;
		c.gridy = 1;
		progressBar = new JProgressBar();
		progressBar.setIndeterminate(true);
		d.add(progressBar, c);
		
		c.gridx = 0;
		c.gridy = 2;		
		cancel = new JButton(language.getButton_cancel());
		cancel.addActionListener(this);
		d.add(cancel, c);
		
		d.setUndecorated(true);
		d.pack();
		d.setVisible(true);
		d.setLocation(m2ng.getLocation().x+m2ng.getWidth()/2-d.getWidth()/2, m2ng.getLocation().y+m2ng.getHeight()/2-d.getHeight()/2);
		m2ng.setEnabled(false);
		
		timer = new Timer(200, this);
		timer.start();
		
		model.genereeriUuedM2ngud(d, timer);
	}
}
